<!--
 * @描述: 
 * @作者: 约訂
 * @Date: 2024-10-31 10:27:48
 * @LastEditTime: 2024-11-19 16:40:45
 * @修改人员: 约訂
-->
<template>
  <div class="chat_box_input">
    <div class="change_main_text">
      <!-- <div class="carrier_item" v-for="(item,index) in carrierData" :key="'carrier_'  + index">
        @{{ item.SendUserName }}：
        <span v-if="item?.ContentType == 0">{{
          item?.Content
        }}</span>
        <img class="image_text"
          v-if="item?.ContentType == 1"
          :src="item?.Content"
        ></img>
        <div class="del_close_box">
          x
        </div>
      </div> -->
      <!-- :type="tag.type" -->
        <el-tag v-for="(item,index) in carrierData" :key="'carrier_'  + index" @close="handleClose(index)"  closable effect="dark"  type="info" class="carrier_item">
      <div v-if="item?.ContentType == 0" class="span_text">{{
          item?.Content
        }}</div>
        <img class="image_text"
          v-if="item?.ContentType == 1"
          :src="item?.Content"
        ></img>
    </el-tag>
    </div>
    <div class="ribbon_box">
      <el-upload  ref="uploadRef" class="upload-demo" :http-request="(row) => {UploadImage(row)}" :show-file-list="false" limit="100">
        <template #trigger>
          <el-icon :size="20"><Picture /></el-icon>
        </template>
      </el-upload>
    </div>
    <div class="input_text">
      <el-input
        v-model="sendData.text"
        style="width: 100%; height: 100%"
        type="textarea"
        placeholder=""
        @keyup.enter="SendText"
      />
    </div>
    <div class="send_btn_list" @click="SendText">发送(Enter)</div>
  </div>
</template>
<script setup lang="ts">
import { toggleDark } from "~/composables";
import { ref, reactive, getCurrentInstance, nextTick, defineEmits } from "vue";
import {
  closeWebSocket,
  initWebSocket,
  sendWebSocket,
  isConnected,
} from "../../../../utils/websocket";
import {
  SocketTypeEnum,
  findEnumNameByValue,
  ToWhomAll,
} from "../../../../utils/socketTypeEnum";
/**element - push 图标 */
import { ossUplaod } from "../../../../utils/ossUpload";
const emit = defineEmits(["GetOldData"]);
const { proxy } = getCurrentInstance() as any;
/**回复组件初始数据*/
const inputFormData = ref<any>({});
/** */
const props = defineProps({
  //   /**需要渲染的所有群 */
  //   groupChatsList: {
  //     type: Array, // 数据类型
  //     default: [], // 未传值时的默认值
  //   },
  /**基础信息 */
  infoData: {
    type: Object, // 数据类型
    default: {}, // 未传值时的默认值
  },
  userData: {
    type: Object, // 数据类型
    default: {}, // 未传值时的默认值
  },
  /**窗口信息 */
  itemData: {
    item: Object,
    default: {
      row: {
        QuestionUserId: 0,
      },
    }, // 未传值时的默认值
  },
});
/**承载问题数据 */
const carrierData = ref<any>([]);
/**发送参数 */
const sendData = ref({
  text: "",
  image: [],
});
/**获取点击的问题 */
const UpText = (row: any, Id: any) => {
  console.log(row);
  if (Id == -1) {
    carrierData.value = [];
  } else {
    carrierData.value[0] = row;
  }
};
/**删除消息 */
const handleClose = (row: any) => {
  console.log(row);
  if (props.itemData?.row.QuestionUserId == -1) {
    carrierData.value.splice(row, 1);
  } else {
    if (carrierData.value.length > 1) {
      carrierData.value.splice(row, 1);
    }
  }
};
/**发送消息 */
const SendText = () => {
  if (
    (sendData.value.text == "" || sendData.value.text == "\n") &&
    sendData.value.image.length == 0
  ) {
    proxy.$ShowMsg({
      message: "不可发送空文本",
      type: "error",
    });
    return;
  }
  console.log(props.itemData?.row, props.userData);

  if (
    props.itemData?.row.QuestionUserId == -1 &&
    carrierData.value.length == 0
  ) {
    sendWebSocket({
      Data: JSON.stringify({
        Id: -1,
        ContentType: 0,
        GroupId: props.itemData?.row?.GroupId,
        GroupInfoId: props.itemData?.row?.GroupInfoId,
        GroupName: props.itemData?.row.GroupName,
        SendUserId: props.userData.UserId,
        SendUserName: props.userData.TrueName,
        SendUserAvatar: "",
        SendRealName: "wechatybot",
        GroupType: props.itemData?.rowGroupType,
        SendCoworker: true,
        Content: sendData.value.text,
      }),
      Token: "",
      Cmd: [
        {
          Type: SocketTypeEnum.AnswerMsg,
          ToWhom: ToWhomAll.ToBot,
          UserId: "",
        },
      ],
    });
    sendData.value.text = "";
    sendData.value.image = [];
    return;
  }
  if (carrierData.value.length == 0) {
    proxy.$ShowMsg({
      message: "请选择一条问题进行回复",
      type: "error",
    });
    return;
  }

  // console.log(props.infoData);
  if (sendData.value.text != "") {
    sendWebSocket({
      Data: JSON.stringify({
        Id: carrierData.value[0].Id,
        ContentId: "",
        QuoteId: "",
        Answer: sendData.value.text,
        Issue: carrierData.value[0].Content,
        IssueUser: carrierData.value[0].SendUserName,
        IssueType: carrierData.value[0].ContentType,
        ContentType: 0,
        UserId: props.userData.UserId,
        NoNeedAnswer: 0,
        GroupName: carrierData.value[0].GroupName,
      }),
      Token: "",
      Cmd: [
        {
          Type: SocketTypeEnum.AnswerMsg,
          ToWhom: ToWhomAll.ToBot,
          UserId: "",
        },
      ],
    });
    sendData.value.text = "";
    sendData.value.image = [];
  }
  // sendData.value;
};
const UploadImage = (row: any) => {
  if (
    props.itemData?.row.QuestionUserId == -1 &&
    carrierData.value.length == 0
  ) {
    ossUplaod(row.file, (row: any) => {
      if (row.Success) {
        console.log(row.url);
        sendWebSocket({
          Data: JSON.stringify({
            Id: -1,
            ContentType: 1,
            GroupId: props.itemData?.row?.GroupId,
            GroupInfoId: props.itemData?.row?.GroupInfoId,
            GroupName: props.itemData?.row.GroupName,
            SendUserId: props.userData.UserId,
            SendUserName: props.userData.TrueName,
            SendUserAvatar: "",
            SendRealName: "wechatybot",
            GroupType: props.itemData?.rowGroupType,
            SendCoworker: true,
            Content: row.rows.url,
            Answer: row.rows.url,
          }),
          Token: "",
          Cmd: [
            {
              Type: SocketTypeEnum.AnswerMsg,
              ToWhom: ToWhomAll.ToBot,
              UserId: "",
            },
          ],
        });
        sendData.value.text = "";
        sendData.value.image = [];
        // this.handleEnter(row.rows.url, item, 1);
      }
    });
    sendData.value.text = "";
    sendData.value.image = [];
    return;
  }
  if (carrierData.value.length == 0) {
    proxy.$ShowMsg({
      message: "请选择一条问题进行回复",
      type: "error",
    });
    return;
  }
  ossUplaod(row.file, (row: any) => {
    if (row.Success) {
      console.log(row.url);
      sendWebSocket({
        Data: JSON.stringify({
          Id: carrierData.value[0].Id,
          ContentId: "",
          QuoteId: "",
          Answer: row.rows.url,
          Issue: carrierData.value[0].Content,
          IssueUser: carrierData.value[0].SendUserName,
          IssueType: carrierData.value[0].ContentType,
          ContentType: 1,
          UserId: props.userData.UserId,
          NoNeedAnswer: 0,
          GroupName: carrierData.value[0].GroupName,
        }),
        Token: "",
        Cmd: [
          {
            Type: SocketTypeEnum.AnswerMsg,
            ToWhom: ToWhomAll.ToBot,
            UserId: "",
          },
        ],
      });
      sendData.value.text = "";
      sendData.value.image = [];
      // this.handleEnter(row.rows.url, item, 1);
    }
  });
};
defineExpose({
  UpText,
});
</script>
<style lang="scss" src="./chat.scss">
</style>